jQuery(document).on("pageinit", "[data-role='page'][data-pagetype='login']", function(event) {
    var page = jQuery(this);
    var email = page.find("#email");
    var password = page.find("#password");
    
    // set cookies for email and password
    email.val($.cookie("wia_email"));
    password.val($.cookie("wia_password"));

    // assign callback to login btn
    page.find("a.login").click(function(event){
        $.ajax({
            type: "POST",
            url: URL_ACTION_LOGIN,
            data: {
                email: email.val(),
                password: password.val(), 
            },

            success: function(msg) {
                $.cookie("wia_email", email.val());
                $.cookie("wia_password", password.val());
                $.mobile.changePage(URL_VIEW_SERVICES, { transition: "slide", reverse: false, changeHash: true });
            },

            error: function(req, status, error) {
                var errmsg = page.find("div.errmsg");
                if (errmsg.length != 0) {
                    errmsg.find("p").html("错误的用户名或者密码！");
                    errmsg.popup("open");
                }
            }
        });
    });
});

